<template>
	<div>
		<el-row>
			<el-col :span="22">
				<el-form :model="formData" ref="formDataRef" :rules="rules" label-width="130px">
					<el-form-item label="项目名称:" prop="ProjectName">
						<el-input placeholder="请输入项目名称" v-model="formData.ProjectName"></el-input>
					</el-form-item>
					<el-form-item label="项目编号:" prop="ProjectNumber">
						<el-input placeholder="请输入项目编号且不可重复" v-model="formData.ProjectNumber"></el-input>
					</el-form-item>
					<el-form-item label="开始时间:" prop="CreateDate">
						<el-date-picker v-model="formData.CreateDate" value-format="yyyy-MM-dd" placeholder="选择年份" style="width: 100%;"></el-date-picker>
					</el-form-item>
					<el-form-item label="项目甲方/合作方:" prop="PartyA">
						<el-select v-model="formData.PartyA" style="width: 100%;" filterable>
							<el-option v-for="item in supplierList" :value="item.Id" :label="item.Name" v-show="!item.Delete"></el-option>
						</el-select>
					</el-form-item>
					<el-form-item label="立项单位:" prop="CompanyId">
						<el-select v-model="formData.CompanyId" style="width: 100%;" filterable>
							<el-option :value="0" label="无"></el-option>
							<el-option v-for="item in companyList" :value="item.Id" v-show="!item.Delete" :label="item.CompanyName"></el-option>
						</el-select>
					</el-form-item>
					<el-form-item align="right">
						<el-button type="primary" style="width: 100%;" @click="insertHandler">添加</el-button>
					</el-form-item>
				</el-form>
			</el-col>
		</el-row>
	</div>
</template>

<script>
	import * as dayjs from 'dayjs'
	export default {
		props: {
			closeFun: {
				type: Function,
				required: true
			},
			isShow:{
				type: Boolean,
				required: true
			},
			companyList:{
				type:Array,
				required:true
			}
		},
		data() {
			return {
				formData: {
					ProjectName: "",
					ProjectNumber: "",
					CreateDate: dayjs(Date()).format('YYYY-MM-DD'),
					PartyA: '',
					CompanyId: 0,
				},
				rules: {
					ProjectName: [{
						required: true,
						message: '请输入项目名称',
						trigger: 'blur'
					}],
					ProjectNumber: [{
						required: true,
						message: '请输入项目编号且不可重复',
						trigger: 'blur'
					}],
					CreateDate: [{
						required: true,
						message: '请选择开始时间',
						trigger: 'change'
					}],
					PartyA: [{
						required: true,
						message: '请选择甲方/合作方',
						trigger: 'change'
					}],
				},
				supplierList:[],
			}
		},
		watch:{
			isShow:{
				handler(value){
					if(value) this.getSupplierList()
				},
				immediate:true,
			}
		},
		methods: {
			insertHandler() {
				this.$refs.formDataRef.validate(valid => {
					if (valid) {
						this.$http.post('/api/Project/Insert/insert', this.formData).then(res => {
							const {
								StatusCode,
								Data,
								Message
							} = res.data || 0
							if (StatusCode == 200) {
								this.$message.success('添加成功')
								this.resetFieldsEvent()
								this.$parent.$parent.dialogAddForm.show = false
								this.closeFun()
							} else {
								this.$message.warning(Message)
							}
						})
					}
				})
			},
			resetFieldsEvent(){
				this.$refs.formDataRef.resetFields()
			},
			getSupplierList() {
				this.$http.post('/api/Values/GetSupplier').then(res => {
					const {
						StatusCode,
						Data,
						Message
					} = res.data || 0
					if (StatusCode == 200) this.supplierList = Data.Data
					else this.supplierList = []
				})
			},
		}
	}
</script>

<style>
</style>